package com.fang.study.api;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;

import java.util.concurrent.CountDownLatch;

/**
 * @author fanglingxiao
 * @version 1.0
 * @description TODO
 * @date 2022/7/6 2:51 下午
 **/
public class Test {
    public static void main(String[] args) throws Exception {
        /**
         * zk是有session概念，没有链接池的概念
         * watch:观察 回调
         * watch的注册发生在 读类型调用 get,exites....
         * 第一类：new zk时候，传入的watch是session级别的，跟path、node没有关系
         */
        System.out.println("hello zookeeper!!!");
        String ips = "101.200.208.140:2181";
        CountDownLatch cd = new CountDownLatch(1);
        ZooKeeper zk = new ZooKeeper(ips, 3000, new Watcher() {
            // 回调方法
            @Override
            public void process(WatchedEvent event) {
                Event.KeeperState state = event.getState();
                Event.EventType type = event.getType();
                String path = event.getPath();
                System.out.println(event.toString());
                switch (state) {
                    case Unknown:
                        break;
                    case Disconnected:
                        break;
                    case NoSyncConnected:
                        break;
                    case SyncConnected:
                        System.out.println("connection!!!");
                        cd.countDown();
                        break;
                    case AuthFailed:
                        break;
                    case ConnectedReadOnly:
                        break;
                    case SaslAuthenticated:
                        break;
                    case Expired:
                        break;
                    default:
                }
                switch (type) {
                    case None:
                        break;
                    case NodeCreated:
                        break;
                    case NodeDeleted:
                        break;
                    case NodeDataChanged:
                        break;
                    case NodeChildrenChanged:
                        break;
                    default:
                }
            }
        });
        cd.await();
        // 和事件回调的state是不一样的
        ZooKeeper.States state = zk.getState();
        switch (state) {
            case CONNECTING:
                System.out.println("ing....");
                break;
            case ASSOCIATING:
                break;
            case CONNECTED:
                System.out.println("end....");
                break;
            case CONNECTEDREADONLY:
                break;
            case CLOSED:
                break;
            case AUTH_FAILED:
                break;
            case NOT_CONNECTED:
                break;
            default:
        }

        String pathName = zk.create("/fang", "hello".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        Stat stat = new Stat();
        // 取数据时，新创建一个watch来监听是否后续数据有变化
        byte[] data = zk.getData("/fang", new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                System.out.println("getData watch: " + watchedEvent.toString());
                try {
                    // true default watch被重新注册(就是new Zookeeper时注册的watch)
                    // this 就是当前的watch
                    zk.getData("/fang", this, stat);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                switch (watchedEvent.getState()) {
                    case Unknown:
                        break;
                    case Disconnected:
                        break;
                    case NoSyncConnected:
                        break;
                    case SyncConnected:
                        System.out.println("callback....");
                        break;
                    case AuthFailed:
                        break;
                    case ConnectedReadOnly:
                        break;
                    case SaslAuthenticated:
                        break;
                    case Expired:
                        break;
                    default:
                }
            }
        }, stat);
        System.out.println(new String(data));
        // 触发watch回调
        Stat stat1 = zk.setData("/fang", "newData1".getBytes(), 0);
        // 此时不会触发回调
        zk.setData("/fang", "newData2".getBytes(), stat1.getVersion());

        System.out.println("-----async start------");
        zk.getData("/fang", false, new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int i, String s, Object o, byte[] bytes, Stat stat) {
                System.out.println("-----async callback------");
                System.out.println(o.toString());
                System.out.println(new String(bytes));
            }
        },"abc");
        System.out.println("-----async end------");

        Thread.sleep(1000000);
    }
}
